﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Three.BookStore.ProductionSystem.ShiftManagement.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Three.BookStore.ProductionSystem.ShiftManagement
{
    public class HolidaySettingServices : ApplicationService, IHolidaySettingServices
    {
        private readonly IRepository<HolidaySettingModel, int> _holidaySettingRepository;

        public HolidaySettingServices(IRepository<HolidaySettingModel, int> holidaySettingRepository)
        {
            _holidaySettingRepository = holidaySettingRepository;
        }

        public async Task<List<HolidaySettingDto>> GetHolidaySettingsAsync(DateTime startDate, DateTime endDate)
        {
            var holidays = await _holidaySettingRepository.GetListAsync(
                h => h.HolidayDate >= startDate && h.HolidayDate <= endDate);

            return ObjectMapper.Map<List<HolidaySettingModel>, List<HolidaySettingDto>>(holidays);
        }

        public async Task<HolidaySettingDto> GetHolidaySettingByDateAsync(DateTime date)
        {
            var holiday = await _holidaySettingRepository.FirstOrDefaultAsync(
                h => h.HolidayDate.Date == date.Date);

            return holiday != null ? ObjectMapper.Map<HolidaySettingModel, HolidaySettingDto>(holiday) : null;
        }

        public async Task<HolidaySettingDto> CreateOrUpdateAsync(CreateOrUpdateHolidaySettingDto input)
        {
            HolidaySettingModel holiday;

            if (input.Id.HasValue && input.Id.Value > 0)
            {
                // 更新
                holiday = await _holidaySettingRepository.GetAsync(input.Id.Value);
                holiday.HolidayDate = input.HolidayDate;
                holiday.HolidayType = input.HolidayType;
                
                await _holidaySettingRepository.UpdateAsync(holiday);
            }
            else
            {
                // 检查是否已存在相同日期的设置
                var existingHoliday = await _holidaySettingRepository.FirstOrDefaultAsync(
                    h => h.HolidayDate.Date == input.HolidayDate.Date);

                if (existingHoliday != null)
                {
                    // 更新现有记录
                    existingHoliday.HolidayType = input.HolidayType;
                    holiday = await _holidaySettingRepository.UpdateAsync(existingHoliday);
                }
                else
                {
                    // 创建新记录
                    holiday = new HolidaySettingModel
                    {
                        HolidayDate = input.HolidayDate,
                        HolidayType = input.HolidayType
                    };
                    
                    holiday = await _holidaySettingRepository.InsertAsync(holiday);
                }
            }

            return ObjectMapper.Map<HolidaySettingModel, HolidaySettingDto>(holiday);
        }

        public async Task DeleteAsync(int id)
        {
            await _holidaySettingRepository.DeleteAsync(id);
        }

        public async Task DeleteByDateAsync(DateTime date)
        {
            var holiday = await _holidaySettingRepository.FirstOrDefaultAsync(
                h => h.HolidayDate.Date == date.Date);

            if (holiday != null)
            {
                await _holidaySettingRepository.DeleteAsync(holiday);
            }
        }
    }
}
